package cn.icare.cms;

import java.security.MessageDigest;

public class MD5Util {
    private static final String hexDigIts[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    /**
     * MD5加密
     *
     * @param origin      字符
     * @param charsetname 编码
     * @return
     */
    public static String MD5Encode(String origin, String charsetname) {
        String resultString = null;
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (null == charsetname || "".equals(charsetname)) {
                resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
            } else {
                resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
            }
        } catch (Exception e) {
        }
        return resultString;
    }


    public static String byteArrayToHexString(byte b[]) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resultSb.append(byteToHexString(b[i]));
        }
        return resultSb.toString();
    }

    public static String byteToHexString(byte b) {
        int n = b;
        if (n < 0) {
            n += 256;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigIts[d1] + hexDigIts[d2];
    }

    public static  void main(String[] args)
    {
       // String msg="appid=wx496a5cc132468b14&attach=4&body=逛呗买菜-订单支付&device_info=web&mch_id=1577595351&nonce_str=7f1a0f64c23d45c58fb15c267eca3dc0&notify_url=https://api.yilugb.com/microservice-mini/rest/notify/wxPayNotify&openid=ohLSX5Pk0kWqKW9eRQaMeqon1KjE&out_trade_no=12020032713062241084604132099230&spbill_create_ip=192.168.149.1&total_fee=1&trade_type=JSAPI&key=8862SJ6ZX9190V66BCJ42VV31K98KM31";
      String msg="appid=wx496a5cc132468b14&attach=22&body=逛呗买菜-订单支付&device_info=web&mch_id=1577595351&nonce_str=318765f75ce540979f5c292cbc47f41f&notify_url=https://api.yilugb.com/microservice-mini/rest/notify/wxPayNotify&openid=ohLSX5Pk0kWqKW9eRQaMeqon1KjE&out_trade_no=12020040220361214985774933488215&spbill_create_ip=172.16.33.220&total_fee=1&trade_type=JSAPI&key=8862SJ6ZX9190V66BCJ42VV31K98KM31";
       String sign= MD5Util.MD5Encode(msg,"UTF-8").toUpperCase();
       System.out.println("sign:"+sign);
    }
}
